﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QLab.IO.Files;
using System.IO;

namespace QLab
{
  /// <summary>
  /// Interaction logic for frmNewProject.xaml
  /// </summary>
  public partial class frmNewProject : Window
  {
    protected const string _error_message =
@"Project and solution names cannot:
- contain any of the system characters
- contain Unicode control characters
- contain surrogate characters

Please enter a valid name.";

    protected string _projectName = String.Empty;
    protected string _solutionName = String.Empty;
    protected string _location = String.Empty;


    public frmNewProject()
    {
      InitializeComponent();
    }

    public string ProjectName { get { return _projectName; } }
    public string SolutionName { get { return _solutionName; } }
    public string Location { get { return _location; } }

    protected void ShowErrorMessage()
    {
      MessageBox.Show(_error_message, "QLab", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }

    private void btnOk_OnClick(object sender, RoutedEventArgs e)
    {
      string projectName = txtProjectName.Text.Trim();
      string solutionName = txtSolutiontName.Text.Trim();
      string location = txtLocation.Text.Trim();

      if (!CustomFile.IsRegularFileName(projectName))
      {
        ShowErrorMessage();
        txtProjectName.Focus();
        return;
      }

      if (!CustomFile.IsRegularFileName(solutionName))
      {
        ShowErrorMessage();
        txtSolutiontName.Focus();
        return;
      }

      if (!CustomFile.IsRegularFilePath(location))
      {
        MessageBox.Show("Not valid location.");
        txtLocation.Focus();
        return;
      }

      if (!Directory.Exists(location))
      {
        if (MessageBox.Show("Selected directory does not exsist. Do you want to create directory?", "QLab",
          MessageBoxButton.YesNo, MessageBoxImage.Warning) == MessageBoxResult.Yes)
        {
          Directory.CreateDirectory(location);
        }
        else return;
      }

      _solutionName = solutionName;
      _projectName = projectName;

      if (!location.EndsWith("\\"))
        location += "\\";
      _location = location;

      DialogResult = true;
    }

    private void btnBrowse_OnClick(object sender, RoutedEventArgs e)
    {
      System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
      dlg.SelectedPath = txtLocation.Text;
      if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
      {
        txtLocation.Text = dlg.SelectedPath;
      }
    }
  }
}
